package com.byx.chatclient.view.activity.ui.notifications;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.byx.chatclient.ChatApp;
import com.byx.chatclient.R;
import com.byx.chatclient.bean.PersonInfoItem;
import com.byx.chatclient.bean.SimpleUserBean;
import com.byx.chatclient.entity.Result;
import com.byx.chatclient.entity.ResultCallback;
import com.byx.chatclient.entity.UserPo;
import com.byx.chatclient.entity.message.Message;
import com.byx.chatclient.event.MessageEvent;
import com.byx.chatclient.factory.RetrofitFactory;
import com.byx.chatclient.http.ActionRequest;
import com.byx.chatclient.im.MessageConverter;
import com.byx.chatclient.im.MessageNotification;
import com.byx.chatclient.view.activity.PersonInfoActivity;
import com.byx.chatclient.view.activity.main.MainActivity;
import com.byx.chatclient.widget.BigImageView;
import com.byx.chatclient.widget.Toolbar;
import com.facebook.drawee.view.SimpleDraweeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import soup.neumorphism.NeumorphButton;

/**
 * @author 34463
 */
@SuppressLint("NonConstantResourceId")
public class MineFragment extends Fragment {

    @BindView(R.id.sdv_avatar)
    protected SimpleDraweeView avatarView;
    private Unbinder mUnbinder;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_notifications, container, false);
        mUnbinder = ButterKnife.bind(this,root);
        initToolBar();
        init(root);
        EventBus.getDefault().register(this);
        return root;
    }

    private void initToolBar() {
        ((MainActivity) requireActivity()).hideToolbarMenu();
        ((MainActivity) requireActivity()).hideToolbarAvatar();
    }

    private void init(View view) {
        UserPo userPo = ChatApp.getInstance().getUserPo();
        avatarView.setImageURI(ChatApp.toSdvPath(userPo.getLocalAvatarUrl()));
        avatarView.setOnClickListener(v -> {
            BigImageView.create(requireContext()).showPhotoViewWithGlide(userPo.getLocalAvatarUrl());
        });
        ((TextView) view.findViewById(R.id.tv_nick_name)).setText(userPo.getNickName());
        ((TextView) view.findViewById(R.id.tv_account)).setText("账号：" + userPo.getLoginId());
    }

    @OnClick({R.id.ll_self_info})
    protected void onClick(View view){
        switch (view.getId()){
            case R.id.ll_self_info:
                startActivity(new Intent(requireContext(), PersonInfoActivity.class));
                break;

            default:break;
        }
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onEvent(MessageEvent event){
        switch (event.getEventType()){
            case AVATAR_CHANGED:
                // 这里需要加上file://，不然会找不到文件
                avatarView.setImageURI(ChatApp.toSdvPath(ChatApp.getInstance().getUserPo().getLocalAvatarUrl()));
            default:
                break;
        }
    }

    /**
     * Called when the view previously created by {@link #onCreateView} has
     * been detached from the fragment.  The next time the fragment needs
     * to be displayed, a new view will be created.  This is called
     * after {@link #onStop()} and before {@link #onDestroy()}.  It is called
     * <em>regardless</em> of whether {@link #onCreateView} returned a
     * non-null view.  Internally it is called after the view's state has
     * been saved but before it has been removed from its parent.
     */
    @Override
    public void onDestroyView() {
        mUnbinder.unbind();
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

}